package com.bright.hxj.cwduijie.service.impl;

import com.bright.ghj.common.util.StringUtil;
import com.bright.hxj.cwduijie.converter.BalanceConverter;
import com.bright.hxj.cwduijie.enums.DataApi;
import com.bright.hxj.cwduijie.enums.SJJHBZ;
import com.bright.hxj.cwduijie.pojo.bo.ApiDataCollection;
import com.bright.hxj.cwduijie.pojo.dto.BalanceDto;
import com.bright.hxj.cwduijie.pojo.po.Balance;
import com.bright.hxj.cwduijie.pojo.query.DuijieQuery;
import com.bright.hxj.cwduijie.pojo.vo.BalanceVO;
import com.bright.hxj.cwduijie.repository.BalanceRepository;
import com.bright.hxj.cwduijie.service.AbstractMonthlyReportApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Component
@RequiredArgsConstructor
@Slf4j
public class ApiBalanceServiceImpl extends AbstractMonthlyReportApiService<BalanceDto, BalanceVO> {

    private final BalanceRepository balanceRepository;
    private final BalanceConverter balanceConverter;


    @Override
    public JpaSpecificationExecutor getJpaSpecificationExecutor() {
        return balanceRepository;
    }

    @Override
    public DataApi getDataApi() {
        return DataApi.BALANCE;
    }

    @Override
    public Function<BalanceDto, Integer> getIdFunc() {
        return BalanceDto::getId;
    }

//    @Override
//    public List<String> getNeedDuijieZtDataKeys() {
//        List<String> dataKeys = balanceRepository.getDistinctZtDataKeyBySjjhbz(SJJHBZ.WAITING.getValue());
//        dataKeys.addAll(balanceRepository.getDistinctZtDataKeyBySjjhbz(SJJHBZ.ERROR.getValue()));
//        return dataKeys;
//    }

    @Override
    public Function<ApiDataCollection, Map<String, List<BalanceDto>>> getDataFunc() {
        return ApiDataCollection::getBalanceMap;
    }

    @Override
    public Function<BalanceDto, BalanceVO> getConverterFunc() {
        return balanceConverter::dtoConvertVO;
    }

    @Override
    protected void getData(ApiDataCollection apiDataCollection, DuijieQuery duijieQuery) {
        List<String> yearMonths = balanceRepository.getDistinctSSQJByZtDataKeyAndSjjhbz(duijieQuery.getZtDataKey(), SJJHBZ.WAITING.getValue());
        yearMonths.addAll(balanceRepository.getDistinctSSQJByZtDataKeyAndSjjhbz(duijieQuery.getZtDataKey(), SJJHBZ.ERROR.getValue()));
        Map<String, List<BalanceDto>> result = new HashMap<>();

        // 20240725 加上判断是否有指定对接年月 若有指定就只返回指定年月的数据 没有就返回全部
        // 比如只指定了2024年 则selectedYearMonth=2024  若指定2024年1月 则=202401
        String selectedYearMonth = getSelectedYearMonth(duijieQuery);

        for (String yearMonth : yearMonths) {
            if (selectedYearMonth == null || StringUtil.startsWith(yearMonth, selectedYearMonth)) {
                // 只推送选定年月的数据
                List<Balance> balances = balanceRepository.getByZTDATAKEYAndSSQJ(duijieQuery.getZtDataKey(), yearMonth);
//            balances.addAll(balanceRepository.getByZTDATAKEYAndSSQJ(duijieQuery.getAccSetDataKey(), yearMonth, SJJHBZ.ERROR.getValue()));
                result.put(yearMonth, balanceConverter.poConvertDTO(balances));
            }

        }

        apiDataCollection.setBalanceMap(result);
    }

    @Override
    public String getYearMonthColumnName() {
        return "SSQJ";
    }
}
